/**
 * Created by commy on 2015/4/30.
 */
var express = require('express'),
    app = module.exports = express(),
    bodyParser = require('body-parser'),
    redis = require('./redis'),
    md5 = require('js-md5'),
    connect = require('./mongodb'),
    merge = require('utils-merge'),
    eventProxy = require('eventproxy'),
    validator = require('validator'),
    models = require('./models'),
    Q = require('q');
// create application/json parser
var jsonParser = require('./jsparser');



app.post('/category/create',jsonParser,function(req,res,next){
    var body = req.body;
    var ep = new eventProxy();
    ep.fail(next);

    var categoryEntity = new models.Category({
        name:body.content.name,
        optime:body.optime,
        count:0,
        changeable:true,
        user:req.userid
    });

    categoryEntity.save(ep.done(function(){
        res.status(200).json({state:'success'});
    }));

});

app.get('/category/list',function(req,res,next){
    var body = req.body;
    var ep = new eventProxy();
    ep.fail(next);

    models.Category.find({user:req.userid}).exec(ep.done(function(results){
        res.status(200).json(results);
    }));

});


app.post('/category/change',jsonParser,function(req,res,next){
    var body = req.body;
    var ep = new eventProxy();
    ep.fail(next);

    models.Category.findOneAndUpdate({
        _id:body._id,
        optime:{
            $lt:body.optime
        }
    },{
        $set:{
            optime:body.optime,
            name:body.content.name
        }
    },ep.done(function(result){
        console.log(result);
        res.status(200).json({state:'success'});
    }));

});


app.post('/category/delete',jsonParser,function(req,res,next){
    var body = req.body;
    var ep = new eventProxy();
    ep.fail(next);

    models.Category.findOneAndRemove({
        _id:body._id,
        optime:{
            $lt:body.optime
        }
    }).exec(ep.done(function(result){
        console.log(result);
        res.status(200).json({state:'success'});
    }));

});

app.post('/clothes/create',jsonParser,function(req,res,next){
    var body = req.body;
    var ep = new eventProxy();
    ep.fail(next);
    var mergeItem = {
        optime:body.optime,
        user:req.userid
    };

    Q.all(body.content.map(function(item){
        var deferred = Q.defer();
        var clothesEntity = new models.Clothes(merge(item,mergeItem));
        clothesEntity.save(function(err,result){
            if(err){
                deferred.reject(new Error(err));
            }else{
                console.log(arguments);
                deferred.resolve(result);
            }
        })
        return deferred.promise;
    })).then(function(results){
        res.status(200).json(results);
    },function(err){
        next(err);
    });
});


app.post('/clothes/delete',jsonParser,function(req,res,next){
    var body = req.body;
    var ep = new eventProxy();
    ep.fail(next);

    models.Clothes.findOneAndRemove({
        _id:body._id,
        optime:{
            $lt:body.optime
        }
    }).exec(ep.done(function(result){
        console.log(result);
        res.status(200).json({state:'success'});
    }));
});


app.post('/clothes/change',jsonParser,function(req,res,next){
    var body = req.body;
    var ep = new eventProxy();
    ep.fail(next);

    models.Clothes.findOneAndUpdate({
        _id:body._id,
        optime:{
            $lt:body.optime
        }
    },{
        $set:merge(body.content,{optime:body.optime})
    },ep.done(function(result){
        console.log(result);
        res.status(200).json({state:'success'});
    }));

});


app.post('/clothes/list',jsonParser,function(req,res,next){
    var body = req.body;
    var ep = new eventProxy();
    ep.fail(next);

    models.Clothes.find({user:req.userid,category:body.category}).exec(ep.done(function(results){
        res.status(200).json(results);
    }));

});